/*
 * ComerciosAgregarFrame.java
 *
 * Created on 16 de abril de 2008, 12:06 PM
 */
package org.bicicletas.registros;    

import org.mybeans.Sesion;
import org.mybeans.ContribuyentesBeans;
import org.mybeans.ComerciosBeans;


import java.sql.ResultSet;
import java.sql.PreparedStatement;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

/**
 *
 * @author  avbravo
 */
public class BiciclesInscripcionAgregarFrame extends javax.swing.JFrame {

    Sesion sesion;
    ContribuyentesBeans contribuyentesBeans;
    ComerciosBeans comerciosBeans;
    String idNew, tipoIdNew;

    /** Creates new form ComerciosAgregarFrame */
    public BiciclesInscripcionAgregarFrame() {
        initComponents();
        /*
         * Fondo
         */

         
         
         

        jPanelFondo.setOpaque(false);
        setContentPane(jPanelFondo);
        /*
         * fondo
         */
        sesion = new Sesion();
        comerciosBeans = new ComerciosBeans();
        contribuyentesBeans = new ContribuyentesBeans();


        java.util.Calendar ca = java.util.Calendar.getInstance();
        java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());
        AutoCompleteDecorator.decorate(this.jComboBoxContribuyente);
        CargarComboContribuyentes();
        AutoCompleteDecorator.decorate(this.jComboBoxComercio);
        CargarComboComercios();
        Nuevo();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanelFondo = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jTextFieldNumero = new javax.swing.JTextField();
        jButtonNuevo = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTextFieldMarca = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextFieldModelo = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jTextFieldColor = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jTextFieldChasis = new javax.swing.JTextField();
        jButtonGuardar = new javax.swing.JButton();
        jButtonLimpiar = new javax.swing.JButton();
        jButtonRegresar = new javax.swing.JButton();
        jLabel13 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jComboBoxContribuyente = new javax.swing.JComboBox();
        jComboBoxComercio = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Agregar Bicicletas");

        jPanelFondo.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Numero");

        jTextFieldNumero.setColumns(35);
        jTextFieldNumero.setEditable(false);
        jTextFieldNumero.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldNumeroActionPerformed(evt);
            }
        });

        jButtonNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/new.jpeg"))); // NOI18N
        jButtonNuevo.setToolTipText("Crear Nuevo");
        jButtonNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonNuevoActionPerformed(evt);
            }
        });

        jLabel2.setText("Marca");

        jLabel3.setText("Modelo");

        jLabel6.setText("Color");

        jLabel4.setText("Chasis");

        jTextFieldChasis.setText("no tiene");

        jButtonGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/save.png"))); // NOI18N
        jButtonGuardar.setToolTipText("Guardar");
        jButtonGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonGuardarActionPerformed(evt);
            }
        });

        jButtonLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/escoba.png"))); // NOI18N
        jButtonLimpiar.setToolTipText("Limpiar");
        jButtonLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLimpiarActionPerformed(evt);
            }
        });

        jButtonRegresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar.setToolTipText("Regresar");
        jButtonRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresarActionPerformed(evt);
            }
        });

        jLabel13.setFont(new java.awt.Font("DejaVu Sans", 1, 13)); // NOI18N
        jLabel13.setText("Dueño");

        jLabel11.setText("Contribuyente");

        jLabel12.setText("Comercio");

        jComboBoxContribuyente.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBoxContribuyenteItemStateChanged(evt);
            }
        });
        jComboBoxContribuyente.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxContribuyenteActionPerformed(evt);
            }
        });

        jComboBoxComercio.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jComboBoxComercioItemStateChanged(evt);
            }
        });
        jComboBoxComercio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBoxComercioActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelFondoLayout = new javax.swing.GroupLayout(jPanelFondo);
        jPanelFondo.setLayout(jPanelFondoLayout);
        jPanelFondoLayout.setHorizontalGroup(
            jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelFondoLayout.createSequentialGroup()
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2)
                            .addComponent(jLabel3)
                            .addComponent(jLabel6)
                            .addComponent(jLabel4))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldColor, javax.swing.GroupLayout.PREFERRED_SIZE, 204, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(jPanelFondoLayout.createSequentialGroup()
                                    .addComponent(jTextFieldNumero, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(jButtonNuevo))
                                .addComponent(jTextFieldModelo)
                                .addComponent(jTextFieldMarca, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE))
                            .addGroup(jPanelFondoLayout.createSequentialGroup()
                                .addGap(19, 19, 19)
                                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanelFondoLayout.createSequentialGroup()
                                        .addGap(13, 13, 13)
                                        .addComponent(jButtonGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 83, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jTextFieldChasis, javax.swing.GroupLayout.PREFERRED_SIZE, 242, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(jPanelFondoLayout.createSequentialGroup()
                                .addComponent(jLabel12)
                                .addGap(41, 41, 41))
                            .addGroup(jPanelFondoLayout.createSequentialGroup()
                                .addComponent(jLabel11)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBoxComercio, javax.swing.GroupLayout.PREFERRED_SIZE, 473, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBoxContribuyente, javax.swing.GroupLayout.PREFERRED_SIZE, 473, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGap(96, 96, 96)
                        .addComponent(jLabel13)))
                .addContainerGap(24, Short.MAX_VALUE))
        );
        jPanelFondoLayout.setVerticalGroup(
            jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelFondoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldNumero, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonNuevo))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldMarca, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldModelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel6)
                    .addComponent(jTextFieldColor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jTextFieldChasis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel13)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jComboBoxContribuyente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel11))
                        .addGap(6, 6, 6)
                        .addComponent(jComboBoxComercio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel12))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonGuardar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelFondo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelFondo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jTextFieldNumeroActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldNumeroActionPerformed
}//GEN-LAST:event_jTextFieldNumeroActionPerformed

private void jButtonRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresarActionPerformed

    dispose();
}//GEN-LAST:event_jButtonRegresarActionPerformed

private void jButtonGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonGuardarActionPerformed


    try {
        String SNumero = this.jTextFieldNumero.getText();

        if (SNumero == null || SNumero.equals("")) {
            JOptionPane.showMessageDialog(this, "Presione el boton Crear Nuevo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldNumero.requestFocus();
            return;
        }
        int IdBicicleta = Integer.parseInt(SNumero);
        if (IdBicicleta <= 0) {
            JOptionPane.showMessageDialog(this, "Presione el boton Crear Nuevo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldNumero.requestFocus();
            return;
        }

        String Marca = this.jTextFieldMarca.getText();
        if (Marca == null || Marca.equals("")) {
            JOptionPane.showMessageDialog(this, "Ingrese la marca", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldMarca.requestFocus();
            return;
        }
        String Modelo = this.jTextFieldModelo.getText();
        if (Modelo == null || Modelo.equals("")) {
            JOptionPane.showMessageDialog(this, "Ingrese el modelo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldModelo.requestFocus();
            return;
        }


        String color = this.jTextFieldColor.getText();
        if (color == null || color.equals("")) {
            JOptionPane.showMessageDialog(this, "Ingrese el color", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldColor.requestFocus();
            return;
        }
        String chasis = this.jTextFieldChasis.getText();
        if (chasis == null || chasis.equals("")) {
            JOptionPane.showMessageDialog(this, "Ingrese el numero de chasis", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldChasis.requestFocus();
            return;
        }
        if (this.idNew == null || this.idNew.equals("")) {
            JOptionPane.showMessageDialog(this, "seleccione el dueño", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jComboBoxContribuyente.requestFocus();
            return;
        }

        if (sesion.Conectar() == false) {
            JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        String sqlquery = "select * from bicicles where idmunicipio = ? and idbicicleta =?";
        PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
        psquery.setString(1, sesion.getIdmunicipio());
        psquery.setInt(2, IdBicicleta);
        ResultSet rs = psquery.executeQuery();
        rs.next();
        int x = rs.getRow();

        if (x != 0) {
            JOptionPane.showMessageDialog(this, "Existe una bicicleta con ese codigo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            sesion.con.close();
            return;
        }
        this.idNew = this.idNew.trim();
        this.tipoIdNew = this.tipoIdNew.trim();

        String sql = "INSERT into bicicles values( ?,?,?,?,?,?,?,?,?,?,?,?)";

        PreparedStatement prest = sesion.con.prepareStatement(sql);

        prest.setInt(1, IdBicicleta);
        prest.setString(2, ""); //placa

        prest.setString(3, Marca);
        prest.setString(4, Modelo);
        prest.setString(5, chasis);
        prest.setString(6, color);
        prest.setString(7, this.idNew);
        prest.setString(8, this.tipoIdNew);
        prest.setString(9, "si");//activo
        prest.setString(10, sesion.getIdmunicipio());
        java.util.Calendar ca = java.util.Calendar.getInstance();
        java.sql.Date mydate = new java.sql.Date(ca.getTimeInMillis());

        prest.setDate(11, mydate);
        prest.setString(12, sesion.getUsername());//hipotecado

        int count = prest.executeUpdate();
        sesion.con.close();
        JOptionPane.showMessageDialog(this, "Inscripcion de bicicleta Guardada exitosamente", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
        jButtonGuardar.setEnabled(false);
    } catch (Exception ex) {

        sesion.MostrarMensajeError("Error", "GuardarBicicletas()\nError " + ex.getMessage().toString());
    }

}//GEN-LAST:event_jButtonGuardarActionPerformed

private void jButtonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLimpiarActionPerformed

    Limpiar();
    Nuevo();

}//GEN-LAST:event_jButtonLimpiarActionPerformed

private void jButtonNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonNuevoActionPerformed

    Nuevo();

}//GEN-LAST:event_jButtonNuevoActionPerformed

    private void Nuevo() {
        try {
            Limpiar();


            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }

            String sqlquery = "select count(*) as total from bicicles where bicicles.idmunicipio = ? ";
            PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
            psquery.setString(1, sesion.getIdmunicipio());

            ResultSet rs = psquery.executeQuery();
            rs.next();
            int total = 0;
            int x = rs.getRow();
            if (x == 0) {
                total = 0;
            } else {
                total = Integer.parseInt(rs.getString("total"));
            }

            total++;
            jTextFieldNumero.setText(String.valueOf(total));
            jButtonGuardar.setEnabled(true);

            repaint();

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
private void jComboBoxContribuyenteItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBoxContribuyenteItemStateChanged
}//GEN-LAST:event_jComboBoxContribuyenteItemStateChanged

private void jComboBoxContribuyenteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxContribuyenteActionPerformed

    try {
        String temp = (String) jComboBoxContribuyente.getSelectedItem();
        if (temp == null || temp.equals("")) {
        } else {
            jComboBoxComercio.setSelectedIndex(0);
            int posicion = temp.indexOf("|");

            int posfinal = 0;
            if (posicion <= 0) {
                posfinal = 0;
            } else {
                posfinal = posicion - 1;
            }

            String contribuyente = temp.substring(0, posfinal);
            int tamano = temp.length();
            int nuevaposicion = posfinal + 2;
            String cedula = temp.substring(nuevaposicion, tamano);
cedula = cedula.trim();
            this.idNew = cedula;
            this.tipoIdNew = "contribuyentes";



        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "Error  " + ex.getMessage(), "ComboBox SubGrupoRegimen ActionPerformed", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jComboBoxContribuyenteActionPerformed

private void jComboBoxComercioItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jComboBoxComercioItemStateChanged
    // TODO add your handling code here:
}//GEN-LAST:event_jComboBoxComercioItemStateChanged

private void jComboBoxComercioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBoxComercioActionPerformed
    // TODO add your handling code here:
    try {
        String temp = (String) jComboBoxComercio.getSelectedItem();
        if (temp == null || temp.equals("")) {
        } else {
            jComboBoxContribuyente.setSelectedIndex(0);
            int posicion = temp.indexOf("|");

            int posfinal = 0;
            if (posicion <= 0) {
                posfinal = 0;
            } else {
                posfinal = posicion - 1;
            }

            String comercio = temp.substring(0, posfinal);
            int posicionF = temp.lastIndexOf("|");

            int posfinalF = 0;
            if (posicionF <= 0) {
                posfinalF = 0;
            } else {
                posfinalF = posicionF - 1;
            }
            int tamano = temp.length();
            int nuevaposicion = posfinalF + 2;
            String codigo = temp.substring(nuevaposicion, tamano);
codigo = codigo.trim();
            this.idNew = codigo;
            this.tipoIdNew = "comercios";

        }
    } catch (Exception ex) {
        JOptionPane.showMessageDialog(this, "Error  " + ex.getMessage(), "ComboBox SubGrupoRegimen ActionPerformed", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jComboBoxComercioActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new BiciclesInscripcionAgregarFrame().setVisible(true);
            }
        });
    }

    private void Limpiar() {
        this.jTextFieldNumero.setText("");
        this.jTextFieldMarca.setText("");
        this.jTextFieldModelo.setText("");
        this.jTextFieldColor.setText("");
        this.jTextFieldChasis.setText("");
        jButtonGuardar.setEnabled(false);


    }

    private void CargarComboContribuyentes() {
        try {
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            String sqlquery = "select contribuyentes.cedula, contribuyentes.contribuyente from contribuyentes where idmunicipio='" + sesion.getIdmunicipio() + "' and activo='si' order by contribuyente";
            PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
            ResultSet rs = psquery.executeQuery();
            jComboBoxContribuyente.removeAllItems();
            int count = 0;
            jComboBoxContribuyente.insertItemAt("", count);
            count = 1;
            while (rs.next()) {
                String contribuyente = rs.getString("contribuyente");
                contribuyente = contribuyente + " | " + rs.getString("cedula");
                jComboBoxContribuyente.insertItemAt(contribuyente, count);
                count++;
            }
            if (count != 0) {
                jComboBoxContribuyente.setSelectedIndex(0);
            }


        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error. CargarSubGrupoRegimen()", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void CargarComboComercios() {
        try {
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            String sqlquery = "select comercios.comercio, comercios.ruc, comercios.idcomercio from comercios where idmunicipio='" + sesion.getIdmunicipio() + "' and activo='si' order by comercio";
            PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
            ResultSet rs = psquery.executeQuery();
            jComboBoxComercio.removeAllItems();
            int count = 0;
            jComboBoxComercio.insertItemAt("", count);
            count = 1;
            while (rs.next()) {
                String comercio = rs.getString("comercio");
                String ruc = rs.getString("ruc");
                if (ruc == null || ruc.equals("")) {
                    ruc = "-";
                }
                comercio = comercio + " | " + ruc + " | " + rs.getString("idcomercio");
                jComboBoxComercio.insertItemAt(comercio, count);
                count++;
            }
            if (count != 0) {
                jComboBoxComercio.setSelectedIndex(0);
            }


        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Metodo:CargarComboComercios()\nError " + ex.getMessage(), "Error.", JOptionPane.ERROR_MESSAGE);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButtonGuardar;
    private javax.swing.JButton jButtonLimpiar;
    private javax.swing.JButton jButtonNuevo;
    private javax.swing.JButton jButtonRegresar;
    private javax.swing.JComboBox jComboBoxComercio;
    private javax.swing.JComboBox jComboBoxContribuyente;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPanelFondo;
    private javax.swing.JTextField jTextFieldChasis;
    private javax.swing.JTextField jTextFieldColor;
    private javax.swing.JTextField jTextFieldMarca;
    private javax.swing.JTextField jTextFieldModelo;
    private javax.swing.JTextField jTextFieldNumero;
    // End of variables declaration//GEN-END:variables
}
